package com.qiaomengapp.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qiaomengapp.bean.ShopGood;
import com.qiaomengapp.bean.ShopWarp;
import com.qiaomengapp.bean.User;
import com.qiaomengapp.exception.CustomException;
import com.qiaomengapp.service.IShopGoodService;
import com.qiaomengapp.service.IShopService;

@Controller
@RequestMapping("shop")
public class ShopController {
	@Resource
	private IShopService service;
	@Resource
	private IShopGoodService goodService;
	
	@RequestMapping("showHtml")
	@ResponseBody
	public ShopWarp showHtml(HttpSession session){
		//得到User对象
		User user = (User) session.getAttribute("user");
		if (user==null) {
			user = new User();
			user.setId(1);
		}
		
		try {
			//根据userid查询shop中的信息
			ShopWarp warp1 = service.select_shopByUid(user.getId());
			ShopWarp warp = service.select_shopByIdForSee(warp1.getId());
			return warp;
		} catch (CustomException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
		return null;
	}
	
	@RequestMapping("showGoodInfo")
	@ResponseBody
	public Map<String, Object> showGoodInfo(Integer shopId){
		try {
			//根据shop_id显示shop中的信息
			ShopWarp warp = service.select_shopByIdForSee(shopId);
			//根据shop_id从shop_good中选出该shop上架的商品
			List<ShopGood> good = goodService.select_ShopGood(shopId);
			//显示shop中的所有商品数量
			int goodNum = goodService.select_shopGoodNum(shopId);
			//显示上新商品数量
			List<Date> time = goodService.select_launchTime();
			SimpleDateFormat format=new SimpleDateFormat();
			int k=0;
			for (Date date : time) {
				int i = goodService.select_shopGoodByTime(format.format(date));
				if(i<=30){
					k++;
				}else{
					
				}
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("warp", warp);
			map.put("good", good);
			map.put("goodNum", goodNum);
			map.put("k", k);
			return map;
		} catch (CustomException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
		return null;
	}
	
	//product-search页面
	@RequestMapping("product-search")
	@ResponseBody
	public List<ShopWarp>  productSearch(String shopName){
		try {
			List<ShopWarp> list = service.select_shopByShopName(shopName);
			//return "product-search.html"为相对于product-search的路径，即url地址栏为/qiaomengapp/shop/product-search.html
			//   "/"回到根目录   选择product-search.html所处的位置    即  /html/product-search.html
			return list;
		} catch (CustomException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
		return null;
		
	}
	

}
